package com.penngo.gui.dfdiff.utils;


import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONConverter;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.awt.*;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Config {
    private static final Logger log = LogUtils.getLogger(Config.class);

    private static final JSONObject config;
//    private static Font defaultFont = new Font("微软雅黑;宋体", Font.PLAIN, 14);
    private static Font defaultFont = new Font("宋体", Font.PLAIN, 14);
    private static boolean isLineWrap = false;

    private final static String STR_LINEWRAP = "linewrap";
    static{
        log.info("init config");
        File file = getConfigFile();
        if(file.exists()){
//            System.out.println("initFont======"+file.getAbsolutePath());
            config = JSONUtil.readJSONObject(file, StandardCharsets.UTF_8);
            JSONObject fontJson = config.getJSONObject("font");
            if (fontJson != null) {
                initFont(fontJson);
            }
//            System.out.println("initFont======"+fontJson.toString());
            isLineWrap = config.getBool(STR_LINEWRAP, false);
        }
        else{
            config = new JSONObject();
        }
    }

    private static File getConfigFile(){
        File dir = ToolUtil.getConfigDir();
        File file = new File(dir, ".config");
        return file;
    }


    private static void initFont(JSONObject fontJson){
        String font = fontJson.getStr("fontName");
        int style = fontJson.getInt("style");
        int size = fontJson.getInt("size");
        defaultFont = new Font(font, style, size);
        //FontUtil.setDefaultFont(defaultFont);
    }

    public static Font getFont(){
        return defaultFont;
    }

    /**
     * 设置和保存默认字体
     * @param font
     */
    public static void setFont(Font font){
        File file = getConfigFile();
        try{
            defaultFont = font;
            String fontName = defaultFont.getName();
            int style = defaultFont.getStyle();
            int size = defaultFont.getSize();

            JSONObject data = new JSONObject();
            data.set("fontName", fontName);
            data.set("style", style);
            data.set("size", size);
            config.set("font", data);
            FileUtil.writeString(JSONUtil.toJsonStr(config), file, "UTF-8");
        }
        catch(Exception e){
            log.log(Level.WARNING, "=====save config error:" + file.getAbsolutePath(), e.getMessage());
        }

    }

    public static boolean isIsLineWrap() {
        return isLineWrap;
    }

    public static void setIsLineWrap(boolean isLineWrap) {
        Config.isLineWrap = isLineWrap;
        File file = getConfigFile();
        try{
            config.set("linewrap", isLineWrap);
            FileUtil.writeString(JSONUtil.toJsonStr(config), file, "UTF-8");
        }
        catch(Exception e){
            log.log(Level.WARNING, "=====save config error:" + file.getAbsolutePath(), e.getMessage());
        }
    }

    public static void main(String[] args) {
//        Map<String, Object> data = new HashMap<>();
//        data.put("fontName", "MicroSoft YaHei UI");
//        JSONObject json = new JSONObject();
//        json.put("fontName", "Microsoft YaHei UI");
//        System.out.println(JSONUtil.toJsonStr(json));
    }
}
